package com.ve.compiler;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.util.Log;

public interface ProgressListener {

    void onSucceed(String info);

    void onFail(String reason, Exception e);

    void onProgress(String detail);

    class ProgressAdapter implements ProgressListener {

        @Override
        public void onSucceed(String info) {
        }

        @Override
        public void onFail(String reason, Exception e) {
        }

        @Override
        public void onProgress(String detail) {
        }
    }

    class LogProgressAdapter implements ProgressListener {
        private static final String TAG = LogProgressAdapter.class.getSimpleName();

        @Override
        public void onSucceed(String info) {
            Log.d(TAG, "onSucceed: " + info);
        }

        @Override
        public void onFail(String reason, Exception e) {
            Log.d(TAG, "onFail: " + reason + "\n" + e);
        }

        @Override
        public void onProgress(String detail) {
            Log.d(TAG, "onProgress: " + detail);
        }
    }

    class DialogProgressAdapter implements ProgressListener {
        private ProgressDialog progressDialog;
        private Context context;
        private String title;


        public DialogProgressAdapter(Context context, String title) {
            this.context = context;
            this.title = title;
        }


        private void runOnUi(Runnable runnable) {
            ((Activity) context).runOnUiThread(runnable);
        }

        @Override
        public void onSucceed(String info) {
            runOnUi(() -> {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                new AlertDialog.Builder(context)
                        .setTitle(title)
                        .setMessage(info)
                        .setPositiveButton("确定", (dialog, which) -> dialog.cancel())
                        .show();


            });

        }

        @Override
        public void onFail(String reason, Exception e) {
            runOnUi(() -> {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                new AlertDialog.Builder(context)
                        .setTitle(title)
                        .setMessage(reason)
                        .setPositiveButton("确定", (dialog, which) -> dialog.cancel())
                        .show();

            });

        }

        @Override
        public void onProgress(String detail) {
            runOnUi(() -> {
                if (progressDialog == null || !progressDialog.isShowing()) {
                    progressDialog = ProgressDialog.show(context, title, detail);
                    progressDialog.setCancelable(false);
                } else {
                    progressDialog.setMessage(detail);
                }
            });

        }

        public void dismissDialog() {
            runOnUi(() -> {
                if (progressDialog != null) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            });
        }
    }

}
